package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Employee;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

import static com.sky.constant.MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL;

/**
 * Author 喵喵喵
 * Date 2025/6/5 14:55
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageCategory(CategoryPageQueryDTO dto) {
        //1.开启分页插件的功能 起始索引=(当前页码 - 1)*每页显示记录数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //2.编写sql语句
        Page<Category> page = categoryMapper.page(dto);
        long total = page.getTotal();
        List<Category> categoryList = page.getResult();

        return new PageResult(total, categoryList);
    }

    @Override
    public void addCategory(CategoryDTO dto) {
        //把dto里面的属性值全部复制到Employee这个对象中
        Category category = new Category();
        //对象的属性拷贝
        BeanUtils.copyProperties(dto, category);
        category.setStatus(StatusConstant.ENABLE);
        //创建时间
        /*category.setCreateTime(LocalDateTime.now());
        //修改时间
        category.setUpdateTime(LocalDateTime.now());
        //创建人
        *//*
        当前这个创建人的id和修改人的id,应该是当前登录人的id
        我们目前在拦截器里已经解析出来了当前登录人的id,现在的问题是
        该如何拿到拦截器中的id
        * *//*
        category.setCreateUser(BaseContext.getCurrentId());
        //修改人
        category.setCreateUser(BaseContext.getCurrentId());*/
        categoryMapper.addCategory(category);
    }

    @Override
    public void StartORStopStatus(Integer status, Integer id) {
        Category category = Category.builder()
                .status(status)
                .id(id.longValue())
                /*.updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();
        categoryMapper.updateCategory(category);
    }

    @Override
    public void deleteCategoryById(Integer id) {
        List<Dish> dishes = dishMapper.getDishListByCategoryId(id);
        if (!dishes.isEmpty())
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        categoryMapper.deleteCategoryById(id);
    }

    @Override
    public void updateCategory(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.updateCategory(category);
    }

    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
